<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    require('../../include/formFunctions.php');
    $filename="";
    include "../../include/dbFunctions.php";
    $id = $_SESSION['vid'];
    $salutation = $_POST['salutation'];
    $surname = htmlspecialchars($_POST['surname'],ENT_QUOTES);
    $firstname = htmlspecialchars($_POST['firstname'],ENT_QUOTES);
    $gender = $_POST['gender'];
    $office = $_POST['office'];
    $mobile = $_POST['mobile'];
    $sponsor = $_POST['sponsor'];
    $job = htmlspecialchars($_POST['job'],ENT_QUOTES);
    $exp = htmlspecialchars($_POST['exp'],ENT_QUOTES);
    $ref = htmlspecialchars($_POST['ref'],ENT_QUOTES);
    $comment = htmlspecialchars($_POST['comment'],ENT_QUOTES);
    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
        //Check if it's size is less than 350Kb
        $filename = newname(basename($_FILES['uploaded_file']['name']));
        $ext = substr($filename, strrpos($filename, '.') + 1);
        if (($_FILES["uploaded_file"]["size"] < 1000000)) {
            //Determine the path to which we want to save this file
            $newname = dirname(__FILE__).'/../../upload/cv/'.$filename;
            //Check if the file with the same name is already exists on the server
            if (!file_exists($newname)) {
                //Attempt to move the uploaded file to it's new place
                if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
                    $_SESSION['mes'] = "File has been uploaded.";
                    //now insert
                } else {
                    $filename="";
                    $_SESSION['mes'] = "Error in file upload. ";
                }
            } else {
                $filename="";
                $_SESSION['mes'] = "Error in file upload.  ";
            }
        } else {
            $filename="";
            $_SESSION['mes'] = "Error: Only file under 1Mb are accepted for upload.   ";
        }
    } else {
        $filename="";
        $_SESSION['mes'] = "No file uploaded.";
    }
//update volunteer table
    $query1 = "UPDATE volunteer
            SET volunteer_surname = '".$surname."',
                volunteer_firstname = '".$firstname."',
                volunteer_mobile = '".$mobile."',
                volunteer_office = '".$office."',
                volunteer_salutation = '".$salutation."',
                volunteer_gender = '".$gender."',
                volunteer_profession = '".$job."',
                volunteer_experience = '".$exp."',
                volunteer_reference = '".$ref."',
                volunteer_cv = '".$filename."',
                volunteer_comment = '".$comment."',
                sponsor_id ='".$sponsor."'
            WHERE volunteer_id=$id";
    $result1 = executeQuery($query1);
    $link = "../volunteerView.php?id=$id";
    if (($result1!=0)) {
        $_SESSION['mes'].="The record has been updated successfully";
    } else {
        $_SESSION['mes'].="Error! Please try again";
    }
    header("Location: $link");
    /*
 * echo $id."<br/>";
echo $salutation."<br/>";
echo $name."<br/>";
echo $gender."<br/>";
echo $email."<br/>";
echo $office."<br/>";
echo $mobile."<br/>";
echo $address."<br/>";
echo $sponsor."<br/>";
print_r($education);
echo"<br/>";
echo $day_p."<br/>";
    */
    ?>
    <?php
}else {
    header("Location: ../include/login.html");
}
?>